package com.zxbit.flash.presenter;

import com.zxbit.flash.base.BasePresenter;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.CalculateMvpView;
import com.zxbit.flash.network.CustomConsumer;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.response.CalculateResponse;
import com.zxbit.flash.bean.ProductData;
import com.zxbit.flash.bean.ProductItemData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import io.reactivex.functions.Consumer;

public class CalculatePresenter extends BasePresenter {
    private CalculateMvpView mvpView;

    public CalculatePresenter(CalculateMvpView mvpView) {
        this.mvpView = mvpView;

    }

//    public void calc(String borrowMoney, String pId) {
//        Map<String, Object> paramsMap = new HashMap<>();
//        paramsMap.put("borrowMoney", borrowMoney);
//        paramsMap.put("pId", pId);
//        toSubScribe(req.getCalculateData(BuildConfig.channel, UserInfo.getToken(), ParamsFactory.getCommonParams(paramsMap)), new CustomConsumer<CalculateResponse>() {
//            @Override
//            public void call(CalculateResponse calculateResponse) {
//                mvpView.onGetDataNext(calculateResponse);
//            }
//        }, new Consumer<Throwable>() {
//            @Override
//            public void accept(Throwable throwable) throws Exception {
//                mvpView.onGetDataError(throwable);
//            }
//        });
//    }

    public void getAllCalculateData(List<ProductItemData> data) {
        List<ProductData> list = new ArrayList<>();
        for (ProductItemData itemData : data) {
            list.add(new ProductData(itemData));
        }
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("products", list);
        toSubScribe(req.getAllCalculateData(UserInfo.getToken(), ParamsFactory.getCommonParams(paramsMap)), new CustomConsumer<CalculateResponse>() {
            @Override
            public void call(CalculateResponse response) {
                mvpView.onGetDataNext(response);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

}
